package com.yssoft.controller.core;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.yssoft.controller.BaseController;
import com.yssoft.entity.core.TCode;
import com.yssoft.service.inspection.InspStandardService;

/**
 * @author 戈超
 * @time 2018/5/18 上午 09:10
 * @  检验管理控制器
 */
@Controller
@RequestMapping(value = "/inspStandard")
public class InspStandardController extends BaseController{
	
	@Resource
	private InspStandardService inspStandardService;
	
	/**
	 * 跳转到检验标准管理列表页
	 * @return
	 */
	@RequestMapping(value = "entList")
	public String list(Model model){
		List<TCode> codeList = inspStandardService.queryCodeList();
		model.addAttribute("codeList", codeList);
		return "inspStandard/inspStandardEntList";
	}
	
	/**
	 * 查询产品检验管理列表
	 * @param page
	 * @param limit
	 * @param sourceName
	 * @param pType
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "queryInspStandardEntList")
	public JSONObject queryInspStandardEntList(@RequestParam(name = "page",defaultValue = "0") int page,
    		@RequestParam(name = "limit",defaultValue = "10") int limit, String sourceName, String pType){
		JSONObject pageList = inspStandardService.queryInspStandardEntPage(page, limit, sourceName, pType);
		return pageList;
	}
	
	/**
	 * 跳转到检验标准管理新增页
	 * @return
	 */
	@RequestMapping(value = "inspectEntEdit")
	public String inspectEntEdit(Integer inspectId, String pType, Model model){
		if(inspectId != null && !"".equals(inspectId)){
			Map<String, Object> source = inspStandardService.queryInspectEntById(inspectId, pType);
			model.addAttribute("source", source);
		}
		return "inspStandard/inspStandardEntForm";
	}
	
	/**
	 * 跳转产品选择页
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "selectSource")
	public String selectSource(Model model){
		List<TCode> codeList = inspStandardService.queryCodeList();
		model.addAttribute("codeList", codeList);
		return "inspStandard/selectSource";
	}
	
	/**
	 * 查询产品列表
	 * @param page
	 * @param limit
	 * @param sourceName
	 * @param pType
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "querySourceList")
	public JSONObject querySourceList(@RequestParam(name = "page",defaultValue = "0") int page,
    		@RequestParam(name = "limit",defaultValue = "10") int limit, String sourceName, String pType){
		JSONObject pageList = inspStandardService.querySourcePage(page, limit, sourceName, pType);
		return pageList;
	}
	
	/**
	 * 编辑保存企业产品检验标准信息
	 * @param sourceId
	 * @param type
	 * @param redirectAttributes
	 * @param name
	 * @param min_value
	 * @param max_value
	 * @param unit
	 * @param insp_step
	 * @param source
	 * @return
	 */
	@RequestMapping(value = "inspStandardSave")
	public String inspStandardSave(Integer sourceId, String type, String delIds, String[] standardEntId, RedirectAttributes redirectAttributes,
			String[] name, String[] min_value, String[] max_value, String[] unit, String[] insp_step, String[] source){
		try {
			inspStandardService.saveInspect(sourceId,type,delIds,standardEntId,name,min_value,max_value,unit,insp_step,source);
			redirectAttributes.addFlashAttribute("message", "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "保存失败!");
		}
		return "redirect:entList";
	}
	
	/**
	 * 删除企业产品检验标准信息
	 * @param inspectId
	 * @param pType
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delInspectEnt")
	public String delInspectEnt(String inspectId, String pType){
		String result = "";
		String[] inspectIdArray = inspectId.split(",");
		String[] pTypeArray = pType.split(",");
		System.out.println(inspectIdArray.length);
		try {
            for (int i = 0; i < inspectIdArray.length; i++) {
            	inspStandardService.delInspect(inspectIdArray[i], pTypeArray[i]);
            }
            result = "success";
        } catch (Exception e) {
            e.printStackTrace();
            result = "error";
        }
		return result;
	}
	
	/**
	 * 跳转企业产品检验标准查看页
	 * @param inspectId
	 * @param pType
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "queryInspectView")
	public String queryInspectView(Integer inspectId, String pType, Model model){
		Map<String, Object> source = inspStandardService.queryInspectEntById(inspectId, pType);
		model.addAttribute("source", source);
		return "inspStandard/inspStandardEntView";
	}
}
